package com.linkage.lcims.base.memoryparam.impl;

import java.util.Iterator;
import java.util.List;

import com.linkage.lcims.base.dao.business.interfaces.ZoneDAO;
import com.linkage.lcims.base.memoryparam.MemoryParamImpl;
import com.linkage.lcims.base.memoryparam.interfaces.ZoneMP;
import com.linkage.lcims.base.po.business.BdZone;
import com.linkage.util.exception.MPException;

public class ZoneMPImpl extends MemoryParamImpl<BdZone> implements ZoneMP {

	private static ZoneMPImpl m_instance = null;
	
	private List<BdZone> zoneList;
	
	private ZoneDAO zoneDAO;
	
	@Override
	public void reload() throws Exception {
		try {
			zoneList = zoneDAO.getAll();
			this.setReload(false);
		} catch (Exception e) {
			throw new MPException(e, this.getClass());
		}
	}
	
	public static ZoneMPImpl getInstance() {
		if (m_instance == null) {
			m_instance = new ZoneMPImpl();
		}
		return m_instance;
	}
	
	
	public BdZone getZone(String zoneid) throws Exception {
		try {
			if (isReload())
				reload();
			Iterator<BdZone> iter = zoneList.iterator();
			BdZone zone = null;
			boolean flag = false;
			while (iter.hasNext()) {
				zone = iter.next();
				if (zone.getZoneid().equals(zoneid)) {
					flag = true;
					break;
				}
			}
			if (flag)
				return zone;
			else
				return null;
		} catch (Exception e) {
			throw new MPException(e, this.getClass());
		}
	}

	public String getZoneName(String zoneid) throws Exception {
		try {
			BdZone zone=this.getZone(zoneid);
			if(zone!=null){
				return zone.getZonename();
			}else{
				return null;
			}
		} catch (Exception e) {
			throw new MPException(e, this.getClass());
		}
	}

	
	public List<BdZone> getAll() throws Exception {
		if (isReload())
			reload();
		return this.zoneList;
	}

	/**
	 * @return the zoneDAO
	 */
	public ZoneDAO getZoneDAO() {
		return zoneDAO;
	}

	/**
	 * @param zoneDAO the zoneDAO to set
	 */
	public void setZoneDAO(ZoneDAO zoneDAO) {
		this.zoneDAO = zoneDAO;
	}

}
